package com.um.psystem.service.faultService.impl;

import com.um.psystem.entity.ExcelHeader;
import com.um.psystem.mapper.platform.faultManageMapper.IFaultDeviceMapper;
import com.um.psystem.mapper.platform.faultManageMapper.IFaultItemMapper;
import com.um.psystem.mapper.platform.faultManageMapper.IFaultLineMapper;
import com.um.psystem.mapper.platform.faultManageMapper.IFaultUnitMapper;
import com.um.psystem.service.faultService.IFaultService;
import com.um.psystem.utils.EasyExcelUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: AiLen2E
 * @Date: 2021/12/21 11:00
 * @version: 1.0
 */
@Service
public class FaultService implements IFaultService {

    @Autowired
    IFaultDeviceMapper faultDeviceMapper;
    @Autowired
    IFaultItemMapper faultItemMapper;
    @Autowired
    IFaultLineMapper faultLineMapper;
    @Autowired
    IFaultUnitMapper faultUnitMapper;

    @Override
    public Integer addDevice(Map map) {
        System.out.print(map);
        return faultDeviceMapper.addDevice(map);
    }

    @Override
    public Integer removeDevice(Integer id) {
        Map<String,Object> map = new HashMap();
        map.put("device_id",id);
        Integer count = faultItemMapper.getItemListCount(map);
        if (count>0)
        {return null;}
        else
        {return faultDeviceMapper.removeDevice(id);}
    }

    @Override
    public Integer changeDevice(Map map) {
        return faultDeviceMapper.changeDevice(map);
    }

    @Override
    public List<Map<String, Object>> getDeviceList(Map map) {
        return faultDeviceMapper.getDeviceList(map);
    }

    @Override
    public Integer addItem(Map map) {
        return faultItemMapper.addItem(map);
    }

    @Override
    public Integer removeItem(Integer id) {
        faultUnitMapper.removeUnitByFaultId(id);
        return faultItemMapper.removeItem(id);
    }

    @Override
    public Integer changeItem(Map map) {
        return faultItemMapper.changeItem(map);
    }

    @Override
    public List<Map<String, Object>> getItemList(Map map) {
        return faultItemMapper.getItemList(map);
    }

    @Override
    public Integer addLine(Map map) {
        return faultLineMapper.addLine(map);
    }

    @Override
    public Integer removeLine(Integer id) {
        Map<String,Object> map = new HashMap();
        map.put("line_id",id);
        List<Map<String,Object>> list = faultDeviceMapper.getDeviceList(map);
        if (list.size()>0)
        {return null;}
        else
        {return faultLineMapper.removeLine(id);}

    }

    @Override
    public Integer changeLine(Map map) {
        return faultLineMapper.changeLine(map);
    }

    @Override
    public List<Map<String, Object>> getLineList(Map map) {
        return faultLineMapper.getLineList(map);
    }

    @Override
    public Integer addUnit(Integer fid, Integer type_dtl_id, Integer unitcount) {
        return faultUnitMapper.addUnit(fid,type_dtl_id,unitcount);
    }

    @Override
    public Integer removeUnit(Integer id) {
        return faultUnitMapper.removeUnit(id);
    }

    @Override
    public Integer changeUnit(Integer id, Integer type_dtl_id, Integer unitcount) {
        return faultUnitMapper.changeUnit(id,type_dtl_id,unitcount);
    }

    @Override
    public List<Map<String, Object>> getUnitList(Map map) {
        return faultUnitMapper.getUnitList(map);
    }

    @Override
    public Integer getItemListCount(Map map) {
        return faultItemMapper.getItemListCount(map);
    }

    @Override
    public Integer addUnitFromBat(List<Map<String, Object>> list) {
        return faultUnitMapper.addUnitFromBat(list);
    }

    @Override
    public void exportExceptionFaultData(HttpServletResponse response, Map map) {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = null;
        try {
            fileName = URLEncoder.encode("机器设备故障记录表", "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
        List<ExcelHeader> headerList = new ArrayList<>();

        headerList.add(new ExcelHeader("n", "序号"));
        headerList.add(new ExcelHeader("line_name", "线体名称"));
        headerList.add(new ExcelHeader("device_name", "设备名称"));
        headerList.add(new ExcelHeader("device_num", "设备编号"));
        ExcelHeader dateBackHeader2 = new ExcelHeader("fault_date", "故障日期");
        dateBackHeader2.setDataType(ExcelHeader.STRING);
        dateBackHeader2.setFormat("yyyy-MM-dd");
        headerList.add(dateBackHeader2);
        headerList.add(new ExcelHeader("fault_phenomenon", "故障现象"));
        headerList.add(new ExcelHeader("fault_type_str", "故障类型"));
        headerList.add(new ExcelHeader("analyse", "故障原因分析"));
        headerList.add(new ExcelHeader("maintenance_measures", "维修措施"));
        headerList.add(new ExcelHeader("units", "故障单元"));
        headerList.add(new ExcelHeader("maintenance_time", "修复时长(min)"));
        headerList.add(new ExcelHeader("maintenance_person", "维修人"));

        List<Map<String, Object>> dataList = getItemList(map);

        for(int k=0;k<dataList.size();k++)
        {
            Map<String, Object> m = dataList.get(k);
            m.put("n",String.valueOf(k+1));
            if (m.containsKey("fault_type"))
            {
                String fault_type_str="";
                switch (m.get("fault_type").toString()) {
                    case "0":fault_type_str= "元器件（物料）问题";break;
                    case "1":fault_type_str= "结构设计问题";break;
                    case "2":fault_type_str= "软件问题";break;
                    case "3":fault_type_str= "装配工艺问题";break;
                    case "4":fault_type_str= "其他问题";break;
                    default:fault_type_str= "未知故障类型";break;
                }
                m.put("fault_type_str",fault_type_str);
            }

            Map units_find_parm = new HashMap();
            units_find_parm.put("fault_id",m.get("fault_id"));
            List<Map<String,Object>> units = getUnitList(units_find_parm);
            String units_str="";
            for (int i=0;i<units.size();i++)
            {
                String fault_unit_name = units.get(i).get("type_dtl_name").toString();
                String unit_count = units.get(i).get("unit_count").toString();

                if (i!=units.size()-1)
                { units_str+=fault_unit_name+"-"+unit_count+"个,"; }
                else
                {units_str+=fault_unit_name+"-"+unit_count+"个";}
            }
            m.put("units",units_str);
        }

        OutputStream excelOutputStream = EasyExcelUtils.exportDataToExcel(headerList, dataList);
        try {
            response.getOutputStream().write(((ByteArrayOutputStream) excelOutputStream).toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Map<String, Object>> getLineTypePieData(Map map) {
        return faultItemMapper.getLineTypePieData(map);
    }

    @Override
    public List<Map<String, Object>> getWorkShopPieData(Map map) {
        return faultItemMapper.getWorkShopPieData(map);
    }
    @Override
    public List<Map<String, Object>> getUnitsPieData(Map map) {
        return faultItemMapper.getUnitsPieData(map);
    }

    @Override
    public List<Map<String, Object>> getFaultTypePieData(Map map) {
        List<Map<String, Object>> datas = faultItemMapper.getFaultTypePieData(map);
        for (Map data:datas)
        {
            if (data.get("name")!=null)
            {
                switch (data.get("name").toString())
                {
                    case "0":data.put("name","元器件（物料）问题");break;
                    case "1":data.put("name","结构设计问题");break;
                    case "2":data.put("name","软件问题");break;
                    case "3":data.put("name","装配工艺问题");break;
                    case "4":data.put("name","其他问题");break;
                    default:data.put("name","未知类型");
                }
            }
        }
        return datas;
    }

    @Override
    public List<Map<String, Object>> getTypeDtlList(Map map) {
        return faultUnitMapper.getTypeDtlList(map);
    }

}
